package com.song.eduservice.controller;


import com.song.commonutils.ResultObj;
import com.song.eduservice.entity.EduCourse;
import com.song.eduservice.entity.vo.CourseInfoVo;
import com.song.eduservice.entity.vo.CoursePublishVo;
import com.song.eduservice.service.EduCourseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author song
 * @since 2020-03-29
 */
@Api(description = "课程管理")
@RestController
@RequestMapping("/eduservice/course")
public class EduCourseController {

    @Autowired
    private EduCourseService eduCourseService;

    /**
     * 分页查询所有课程信息
     * @param eduCourse
     * @return
     */
    @ApiOperation(value = "分页查询所有课程信息")
    @PostMapping("/findAllCourse/{current}/{limit}")
    public ResultObj findAllCourse(@PathVariable long current,@PathVariable long limit,
                                   @RequestBody(required = false)  EduCourse eduCourse) {
        return eduCourseService.findAllCourse(current,limit,eduCourse);
    }
    /**
     * 添加课程信息
     *
     * @param courseInfoVo
     * @return
     */
    @ApiOperation(value = "添加课程信息")
    @PostMapping("/")
    public ResultObj addCourse(@RequestBody CourseInfoVo courseInfoVo) {
        String result = eduCourseService.saveCourse(courseInfoVo);
            return ResultObj.ADD_SUCCESS.data("id",result);
    }

    /**
     * 根据id查询课程信息
     * @param courseId
     * @return
     */
    @ApiOperation(value = "根据id查询课程信息")
    @GetMapping("/findCourseByCourseId/{courseId}")
    public ResultObj findCourseByCourseId(@PathVariable String courseId){
        CourseInfoVo courseInfo = eduCourseService.findCourseByCourseId(courseId);
        return ResultObj.ok().data("data",courseInfo);
    }

    /**
     * 修改课程信息
     * @param courseInfoVo
     * @return
     */
    @ApiOperation(value = "修改课程信息")
    @PutMapping("/")
    public ResultObj updateCourse(@RequestBody CourseInfoVo courseInfoVo){
        int result = eduCourseService.updateCourseInfo(courseInfoVo);
        if(result>0){
            return ResultObj.UPDATE_SUCCESS;
        }
        return ResultObj.UPDATE_ERROR;
    }

    /**
     * 根据课程id查询确认课程
     * @param courseId
     * @return
     */
    @ApiOperation(value = "根据课程id查询确认课程")
    @GetMapping("/getPublishCourseInfo/{courseId}")
    public ResultObj getPublishCourseInfo(@PathVariable String courseId){
        CoursePublishVo coursePublishVo=eduCourseService.getPublishCourseInfo(courseId);
        return ResultObj.ok().data("data",coursePublishVo);
    }

    /**
     * 发布课程
     * @param id
     * @return
     */
    @ApiOperation(value = "发布课程")
    @PutMapping("publishCourseInfo/{id}")
    public ResultObj publishCourseInfo(@PathVariable String id){
        EduCourse eduCourse=new EduCourse();
        eduCourse.setId(id);
        eduCourse.setStatus("Normal");
        boolean b = eduCourseService.updateById(eduCourse);

        if(b){
            return ResultObj.PUBLISH_SUCCESS;
        }
        return ResultObj.PUBLISH_ERROR;
    }

    /**
     * 根据课程id删除课程
     * @param courseId
     * @return
     */
    @ApiOperation(value = "根据课程id删除课程")
    @DeleteMapping("/{courseId}")
    public ResultObj deleteCourse(@PathVariable String courseId){
        int result=eduCourseService.deleteCourse(courseId);
        if(result>0){
            return ResultObj.DELETE_SUCCESS;
        }
        return ResultObj.DELETE_ERROR;
    }
}

